<template>
  <el-dialog
    top="30px"
    width="1000px"
    append-to-body
    :close-on-click-modal="false"
    :before-close="beforeClose"
    :visible.sync="visible"
  >
    <el-button @click="changeAttachStatus" :loading="submitLoading">同步调查附件</el-button>
    <el-button @click="changeLoanStatus" :loading="submitLoading">同步放款附件</el-button>
    <el-tabs>
      <el-tab-pane label="立项附件">
        <attach-main :related-id="queryId"></attach-main>
      </el-tab-pane>
      <el-tab-pane label="调查附件">
        <approve-related :related-data="relatedData"></approve-related>
      </el-tab-pane>
      <el-tab-pane label="风险审查附件">
        <attach-risk-main :related-id="queryId"></attach-risk-main>
      </el-tab-pane>
      <el-tab-pane label="合同审批附件">
        <span>合同审批的附件未存在关联表，需手动点击保存</span>
        <contract-attach :query-id="queryId"></contract-attach>
      </el-tab-pane>
      <el-tab-pane label="放款审批附件">
        <loan-attach :related-id="queryId"></loan-attach>
      </el-tab-pane>
    </el-tabs>
  </el-dialog>
</template>

<script>
import AttachMain from '@/credit/views/city/bus/normal/item/AttachMain'
import ApproveRelated from '@/credit/views/county/unionProvince/components/projectApprove/ApproveRelated'
import AttachRiskMain from '@/credit/views/county/unionProvince/components/projectApprove/components/risk/AttachMain'
import ContractAttach from '@/cadmin/views/org/components/ContractAttach'
import LoanAttach from '@/credit/views/city/bus/normal/loan/AttachMain'
export default {
  components: {
    AttachMain,
    AttachRiskMain,
    ContractAttach,
    LoanAttach,
    ApproveRelated,
  },
  props: {
    visible: Boolean,
    queryId: String,
  },
  data() {
    return {
      submitLoading: false,
      loanApprove: {},
      relatedData: {},
    }
  },
  computed: {
    loanApproveId() {
      return this.loanApprove.loan_approval_id
    },
  },
  watch: {
    queryId: {
      handler: function () {
        this.getLoanApprove()
      },
      immediate: true,
    },
  },
  created() {},
  mounted() {},
  methods: {
    async getLoanApprove() {
      if (!this.queryId) {
        return
      }
      this.submitLoading = true
      const res = await this.$api.getDataFromId('credit', 'customer_related', this.queryId)
      this.relatedData = res.data ?? {}
      this.$api
        .getData('credit', 'loan_approval', { 'customer_related_id.eq': this.queryId })
        .then(res => {
          this.loanApprove = res?.data?.[0] ?? {}
        })
        .finally(() => {
          this.submitLoading = false
        })
    },
    beforeClose() {
      this.$emit('update:visible', false)
    },
    changeAttachStatus() {
      this.submitLoading = true
      this.$api
        .updateData(this.$serviceName, 'customer_related', this.queryId, { is_supply_datum: 1 })
        .then(() => {
          this.$message.success('同步调查附件成功')
        })
        .finally(() => {
          this.submitLoading = false
        })
    },
    changeLoanStatus() {
      this.submitLoading = true
      this.$api
        .updateData(this.$serviceName, 'loan_approval', this.loanApproveId, {
          is_supply_datum: 1,
          customer_related_id: this.queryId,
        })
        .then(() => {
          this.$message.success('同步放款附件成功')
        })
        .finally(() => {
          this.submitLoading = false
        })
    },
  },
}
</script>

<style lang="scss" scoped></style>
